package com.acim.mobile
{
	import com.client.DLLLoader;
	import com.client.XMLLoader;
	import com.client.event.VerEvent;
	import com.client.event.XMLEvent;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	import flash.utils.getDefinitionByName;
	
	/**
	 * ACIM客户端
	 * @author Tide
	 */
	[SWF(width=780, height = 575, frameRate = 24)]
	public class HomeView extends Sprite
	{
		[Embed(source="/../config/dll.xml",mimeType="application/octet-stream")]
		private static var _dllClass:Class;
		
		private static const DLL_XML:String = "config/dll.xml";
		private static const SERVER_XML:String = "config/Server.xml";
		
		private var _xmlLoader:XMLLoader;
		private var _dllLoader:DLLLoader;
		
		//xml
		private var _dllXML:XML;
		private var _configXML:XML;
		private var _updateXML:XML;
		
		private var _version:String;
		private var _verMap:Object = {};
		private var _isRelease:Boolean = false;
		
		public function HomeView()
		{
			initStage();
			getTokenFromPage();
			setPlayerContent();
			start();
			/*graphics.beginFill(0xff0000,.5);
			graphics.drawCircle(300,300,150);
			graphics.endFill();*/
		}
		
		/**
		 * 初始化舞台
		 */
		private function initStage():void
		{
			
		}
		
		/**
		 * 从页面中获取参数
		 */
		private function getTokenFromPage():void
		{
			
		}
		
		/**
		 * 设置Player
		 */
		private function setPlayerContent():void
		{
			
		}
		
		/**
		 * 正式开始:
		 * loading-->dll.xml-->Server.xml-->version.swf-->dll-->setup
		 */
		private function start():void
		{
			loadXMLResource();
		}
		
		private function loadXMLResource():void
		{
			trace("加载dll.xml");
			_xmlLoader = new XMLLoader();
			_xmlLoader.addEventListener(XMLEvent.COMPLETE,onDllXMLComplete);
			_xmlLoader.load(DLL_XML + "?" + Math.random());
		}
		
		private function onDllXMLComplete(event:XMLEvent):void
		{
			_xmlLoader.removeEventListener(XMLEvent.COMPLETE,onDllXMLComplete);
			_dllXML = event.data;
			
			trace("加载Server.xml");
			_xmlLoader.addEventListener(XMLEvent.COMPLETE,onServerXMLComplete);
			_xmlLoader.load(SERVER_XML + "?" + Math.random());
		}
		
		private function onServerXMLComplete(event:XMLEvent):void
		{
			_xmlLoader.removeEventListener(XMLEvent.COMPLETE,onServerXMLComplete);
			
			var array:Array = (event.data.toString()).split("****");
			_configXML = XML(array[0]);
			_updateXML = XML(array[1]);
			
			loadDLL();
		}
		
		private function loadDLL():void
		{
			_dllLoader = new DLLLoader(_version);
			_dllLoader.addEventListener(Event.OPEN,onDLLOpen);
			_dllLoader.addEventListener(ProgressEvent.PROGRESS,onLoadingProgress);
			_dllLoader.addEventListener(Event.COMPLETE, onDLLComplete);
			_dllLoader.addEventListener(VerEvent.SIZE_ERROR, onVerSizeError);
			_dllLoader.doLoad(_dllXML.elements("Dll")[0],_verMap,_isRelease);
		}
		
		private function onDLLOpen(event:Event):void
		{
			
		}
		
		private function onLoadingProgress(event:ProgressEvent):void
		{
			
		}
		
		private function onDLLComplete(event:Event):void
		{
			trace("DLL加载完成");
			_dllLoader.removeEventListener(Event.OPEN,onDLLOpen);
			_dllLoader.removeEventListener(VerEvent.SIZE_ERROR, onVerSizeError);
			_dllLoader.removeEventListener(ProgressEvent.PROGRESS,onLoadingProgress);
			_dllLoader.removeEventListener(Event.COMPLETE, onDLLComplete);
			_dllLoader = null;
			
			//初始化相关类
			var clientConfig:* = getDefinitionByName("com.acim.core.config.ClientConfig");
			clientConfig.setup(_configXML);
			
			var mClass:Class = getDefinitionByName("com.acim.app.MainEntry") as Class;
			var mainEntry:* = new mClass();
			mainEntry.setup(this);
		}
		
		private function onVerSizeError(event:VerEvent):void
		{
			
		}
	}
}